package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProductRecordsRpcService;
import com.hunttown.mes.rpc.domain.MesProductRecordsDTO;
import com.hunttown.mes.rpc.domain.query.MesProductRecordsDTOQuery;
import com.hunttown.mes.service.MesProductRecordsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProductRecordsConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:34:23
 */
@Service
public class MesProductRecordsRpcServiceImpl implements MesProductRecordsRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProductRecordsRpcServiceImpl.class);

    private final MesProductRecordsService mesProductRecordsService;

    @Autowired
    public MesProductRecordsRpcServiceImpl(MesProductRecordsService mesProductRecordsService) {
        this.mesProductRecordsService = mesProductRecordsService;
    }

    @Override
    public MesProductRecordsDTO insert(MesProductRecordsDTO mesProductRecordsDTO) {
        try {
            return convert(mesProductRecordsService.insert(convert(mesProductRecordsDTO)));
        } catch (Exception e) {
            logger.error("MesProductRecords create failed.", e);
            throw new RuntimeException("MesProductRecords create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProductRecordsDTO mesProductRecordsDTO) {
        try {
            return mesProductRecordsService.updateInfoById(convert(mesProductRecordsDTO));
        } catch (Exception e) {
            logger.error("MesProductRecords update by id failed.", e);
            throw new RuntimeException("MesProductRecords update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProductRecordsDTOQuery mesProductRecordsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProductRecordsDTOQuery.getW_id() != null && mesProductRecordsDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProductRecordsDTOQuery.getW_id());
            }

            if (mesProductRecordsDTOQuery.getIds() != null && !mesProductRecordsDTOQuery.getIds().equals("")) {
                query.put("ids", mesProductRecordsDTOQuery.getIds());
            }

            if (mesProductRecordsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProductRecordsDTOQuery.getStopFlag());
            }

            if (mesProductRecordsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProductRecordsDTOQuery.getDeleteFlag());
            }

            return mesProductRecordsService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProductRecords update by query failed.", e);
            throw new RuntimeException("MesProductRecords update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProductRecordsDTO selectById(Integer id) {
        try {
            return convert(mesProductRecordsService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProductRecords select by id failed.", e);
            throw new RuntimeException("MesProductRecords select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProductRecordsDTO selectObjByQuery(MesProductRecordsDTOQuery mesProductRecordsDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProductRecordsDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProductRecordsDTOQuery.getStopFlag());
            }

            if (mesProductRecordsDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProductRecordsDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProductRecordsDTOQuery.getOrderBy());
            }

            return convert(mesProductRecordsService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProductRecords select obj by query failed.", e);
            throw new RuntimeException("MesProductRecords select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProductRecordsDTO> getListForPage(MesProductRecordsDTOQuery mesProductRecordsDTOQuery) {
        Query query = Query.page(mesProductRecordsDTOQuery.getPn(), mesProductRecordsDTOQuery.getPageSize());

        if (mesProductRecordsDTOQuery.getId() != null && mesProductRecordsDTOQuery.getId() > 0) {
            query.put("id", mesProductRecordsDTOQuery.getId());
        }

        if (mesProductRecordsDTOQuery.getIds() != null && !mesProductRecordsDTOQuery.getIds().equals("")) {
            query.put("ids", mesProductRecordsDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getLikeName())) {
            query.put("likeName", mesProductRecordsDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getShowDate())) {
            query.put("showDate", mesProductRecordsDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getStartDate())) {
            query.put("startDate", mesProductRecordsDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getEndDate())) {
            query.put("endDate", mesProductRecordsDTOQuery.getEndDate());
        }

        if (mesProductRecordsDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProductRecordsDTOQuery.getStopFlag());
        }

        if (mesProductRecordsDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProductRecordsDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProductRecordsDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProductRecordsDTOQuery.getOrderBy());
        }

        List<MesProductRecordsDTO> list = convert(mesProductRecordsService.selectByQuery(query));
        Page<MesProductRecordsDTO> page = Page.from(mesProductRecordsDTOQuery.getPn(), mesProductRecordsDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProductRecordsService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProductRecordsService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProductRecords delete failed.", e);
            throw new RuntimeException("MesProductRecords delete failed, Please order your code.", e);
        }
    }
}